package com.cssw.fyzb.tps.api.pay;

import com.cssw.fyzb.tps.service.pay.WxPayService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: chenchao
 * @Date: 2020/7/14 22:10
 */

@Function(description = "微信app下单", parameters = {
        @Parameter(name = "body", type = Type.String, description = "内容-实名认证费用", required = true),
        @Parameter(name = "subject", type = Type.String, description = "主题", required = true),
        @Parameter(name = "outTradeNo", type = Type.String, description = "外部单号", required = true),
        @Parameter(name = "amount", type = Type.String, description = "金额", required = true)
})
@Component
public class WxAppPay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(AliAppPay.class);

    @Autowired
    private WxPayService wxPayService;


    @Override
    public Response execute(Request request) throws Exception {
        String body = request.getString("body");
        String subject = request.getString("subject");
        String outTradeNo = request.getString("outTradeNo");
        String amount = request.getString("amount");

        logger.debug("入参:body:{},subject:{},outTradeNo:{},amount:{}", body, subject, outTradeNo, amount);
        Map result = wxPayService.appPay(outTradeNo, body, subject, amount);

        if (result == null) {
            return Response.fail("下单失败");
        }
        return Response.success().put("data", result).put("tradeNo", outTradeNo);
    }
}
